<template>
  <div class="app-container">
    <ResizeBox :treeData="treeData" @pitchOnData="pitchOnData">
      <el-tabs
        type="card"
        v-model="queryParams.checkStatus"
        @tab-click="handleQuery"
      >
        <el-tab-pane label="未排查" name="0"></el-tab-pane>
        <el-tab-pane label="待确认" name="2"></el-tab-pane>
        <el-tab-pane label="回退" name="5"></el-tab-pane>
        <el-tab-pane label="不采纳" name="3"></el-tab-pane>
        <el-tab-pane label="转派" name="4"></el-tab-pane>
        <el-tab-pane label="已排查" name="1"></el-tab-pane>
      </el-tabs>

      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-form-item label="隐患来源" prop="source">
          <el-select v-model="queryParams.source" placeholder="请选择隐患来源">
            <el-option
              v-for="item in dict.type.danger_source"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际检查人" prop="checkBy">
          <el-select
            v-model="queryParams.checkBy"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="selectUserList"
            :loading="loadingUser"
          >
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排查时间" prop="checkTimes">
          <el-date-picker
            v-model="queryParams.checkTimes"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
      </el-form>

      <!-- 按钮组 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['syf:record:add']"
            >新增</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['syf:record:edit']"
            >修改</el-button
          >
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['syf:record:remove']"
            >删除</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['syf:record:export']"
            >导出</el-button
          >
        </el-col> -->
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="recordList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="安全风险对象"
          align="center"
          prop="objectName"
          show-overflow-tooltip
        />
        <el-table-column
          label="安全风险单元"
          align="center"
          prop="unitName"
          show-overflow-tooltip
        />
        <el-table-column label="隐患来源" align="center" prop="source" />
        <el-table-column
          label="排查内容"
          align="center"
          prop="content"
          show-overflow-tooltip
        />
        <el-table-column
          label="检查人"
          align="center"
          prop="checkByNames"
          show-overflow-tooltip
        />
        <el-table-column
          label="排查开始时间"
          align="center"
          prop="checkBeginTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.checkBeginTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="排查终止时间"
          align="center"
          prop="checkEndTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.checkEndTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="实际检查人" align="center" prop="checkByName" />
        <el-table-column
          label="排查时间"
          align="center"
          prop="checkTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.checkTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="排查结果" align="center" prop="result">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.result == '0'"
              >无隐患</el-tag
            >
            <el-tag type="danger" v-else-if="scope.row.result == '1'"
              >存在隐患</el-tag
            >
            <el-tag type="warning" v-else>未排查</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          :width="fixedWidth"
        >
          <template slot-scope="scope">
            <el-button
              plain
              size="small"
              type="primary"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['syf:record:edit']"
              v-if="queryParams.checkStatus == '5'"
              >修改</el-button
            >
            <el-button
              plain
              size="small"
              type="primary"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['syf:record:edit']"
              v-if="queryParams.checkStatus == '0'"
              >排查</el-button
            >
            <el-button
              plain
              size="small"
              type="success"
              @click="confirmById(scope.row.id)"
              v-hasPermi="['syf:record:edit']"
              v-if="
                queryParams.checkStatus == '2' || queryParams.checkStatus == '4'
              "
              >隐患确认</el-button
            >
            <el-button
              plain
              size="small"
              type="danger"
              @click="noAdoptionById(scope.row.id)"
              v-hasPermi="['syf:record:edit']"
              v-if="
                queryParams.checkStatus == '2' || queryParams.checkStatus == '4'
              "
              >不采纳</el-button
            >
            <el-button
              plain
              size="small"
              type="warning"
              @click="handleUpdate2(scope.row)"
              v-hasPermi="['syf:record:edit']"
              v-if="
                queryParams.checkStatus == '2' || queryParams.checkStatus == '4'
              "
              >转派</el-button
            >
            <el-button
              plain
              size="small"
              type="info"
              @click="backById(scope.row.id)"
              v-hasPermi="['syf:record:edit']"
              v-if="
                queryParams.checkStatus == '2' || queryParams.checkStatus == '4'
              "
              >回退</el-button
            >
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
              v-hasPermi="['syf:record:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改排查记录对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        width="1000px"
        append-to-body
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="隐患来源" prop="source">
                <el-select
                  v-model="form.source"
                  placeholder="请选择隐患来源"
                  :disabled="formDisabled"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in dict.type.danger_source"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="安全风险对象" prop="objectId">
                <el-select
                  v-model="form.objectId"
                  filterable
                  placeholder="请选择安全风险对象"
                  :disabled="formDisabled"
                  @change="getUnitDate"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in objectDate"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="安全风险单元" prop="unitId">
                <el-select
                  v-model="form.unitId"
                  filterable
                  placeholder="请选择安全风险单元"
                  :disabled="formDisabled"
                  @change="setImei"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in unitDate"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="IMEI号" prop="imei">
                <el-input v-model="form.imei" placeholder="请输入IMEI号" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="排查内容" prop="content">
                <el-input
                  v-model="form.content"
                  type="textarea"
                  placeholder="请输入排查内容"
                  :disabled="formDisabled"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="checkDept">
                <treeselect
                  v-model="form.checkDept"
                  :options="deptOptions"
                  :show-count="true"
                  placeholder="请选择部门"
                  :disabled="formDisabled"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="检查人" prop="checkBy">
                <UserSelect
                  :userId="form.checkBy"
                  :deptId="form.checkDept"
                  @upData="
                    (val) => {
                      form.checkBy = val;
                    }
                  "
                  placeholder="请选择检查人"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="排查时间" prop="checkTime">
                <el-date-picker
                  v-model="form.checkTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="pickerOptions"
                  placeholder="请选择排查时间"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排查结果" prop="result">
                <el-select
                  v-model="form.result"
                  placeholder="请选择排查结果"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in resultDate"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <div v-if="form.result == '1'">
            <el-row>
              <el-col :span="12">
                <el-form-item label="隐患名称" prop="dangerName">
                  <el-input
                    v-model="checkDangerForm.dangerName"
                    placeholder="请输入隐患名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="隐患类型" prop="hazardDangerType">
                  <el-select
                    v-model="checkDangerForm.hazardDangerType"
                    placeholder="请选择隐患类型"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in dict.type.danger_type"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="隐患等级" prop="dangerLever">
                  <el-select
                    v-model="checkDangerForm.dangerLever"
                    placeholder="请选择隐患等级"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in dangerLeverDate"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="治理类型" prop="governanceType">
                  <el-select
                    v-model="checkDangerForm.governanceType"
                    placeholder="请选择治理类型"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in governanceTypeDate"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item label="隐患描述" prop="dangerDescribe">
                  <el-input
                    v-model="checkDangerForm.dangerDescribe"
                    type="textarea"
                    placeholder="请输入隐患描述"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item label="原因分析" prop="cause">
                  <el-input
                    v-model="checkDangerForm.cause"
                    type="textarea"
                    placeholder="请输入原因分析"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="责任部门" prop="fixDept">
                  <treeselect
                    v-model="checkDangerForm.fixDept"
                    :options="deptOptions"
                    :show-count="true"
                    placeholder="请选择责任部门"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="责任人" prop="fixBy">
                  <UserSelect
                    :userId="checkDangerForm.fixBy"
                    :deptId="checkDangerForm.fixDept"
                    @upData="
                      (val) => {
                        checkDangerForm.fixBy = val;
                      }
                    "
                    placeholder="请选择责任人"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="整改限期时间" prop="endFixTime">
                  <el-date-picker
                    v-model="checkDangerForm.endFixTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :picker-options="pickerOptions"
                    placeholder="请选择整改限期时间"
                    style="width: 100%"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="转派"
        :visible.sync="open2"
        width="500px"
        append-to-body
      >
        <el-form ref="form2" :model="checkDangerForm" label-width="120px">
          <el-form-item label="责任部门" prop="fixDept">
            <treeselect
              v-model="checkDangerForm.fixDept"
              :options="deptOptions"
              :show-count="true"
              placeholder="请选择责任部门"
            />
          </el-form-item>
          <el-form-item label="责任人" prop="fixBy">
            <UserSelect
              :userId="checkDangerForm.fixBy"
              :deptId="checkDangerForm.fixDept"
              @upData="
                (val) => {
                  checkDangerForm.fixBy = val;
                }
              "
              placeholder="请选择责任人"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm2">确 定</el-button>
          <el-button @click="cancel2">取 消</el-button>
        </div>
      </el-dialog>
    </ResizeBox>
  </div>
</template>

<script>
import {
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateRecord,
  check,
  backById,
  confirmById,
  noAdoptionById,
  forwardById,
} from "@/api/syf/check/record";
import { listUser } from "@/api/system/user";
import { unitTree } from "@/api/syf/tree";
import { listObject } from "@/api/syf/risk/object";
import { listUnit } from "@/api/syf/risk/unit";
import ResizeBox from "@/components/syf/ResizeBox/index";
import UserSelect from "@/components/syf/UserSelect/index";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Record",
  components: { ResizeBox, Treeselect, UserSelect },
  dicts: ["danger_source", "danger_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingUser: false,
      formDisabled: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排查记录表格数据
      recordList: [],
      // 单元树
      treeData: [],
      // 部门树选项
      deptOptions: undefined,
      //用户集合
      userList: [],
      objectDate: [],
      unitDate: [],
      resultDate: [
        { label: "无隐患", value: "0" },
        { label: "存在隐患", value: "1" },
      ],
      governanceTypeDate: [
        { label: "限期", value: "0" },
        { label: "即查即改", value: "1" },
      ],
      dangerLeverDate: [
        { label: "普通隐患", value: "0" },
        { label: "重大隐患", value: "1" },
      ],
      //列表操作列列宽
      fixedWidth: 150,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitIds: [],
        checkStatus: "0",
        source: null,
        source: null,
        checkBy: null,
        checkTimes: [],
      },
      // 表单参数
      form: {},
      //隐患参数
      checkDangerForm: {},
      // 表单校验
      rules: {
        source: [
          { required: true, message: "请选择隐患来源", trigger: "blur" },
        ],
        objectId: [
          { required: true, message: "请选择分析对象", trigger: "blur" },
        ],
        unitId: [
          { required: true, message: "请选择分析单元", trigger: "blur" },
        ],
        checkDept: [{ required: true, message: "请选择部门", trigger: "blur" }],
        checkBy: [{ required: true, message: "请选择检查人", trigger: "blur" }],
        result: [
          { required: true, message: "请选择排查结果", trigger: "blur" },
        ],
        checkTime: [
          { required: true, message: "请选择排查时间", trigger: "blur" },
        ],
        content: [
          { required: true, message: "请输入排查内容", trigger: "blur" },
        ],
        imei: [
          {
            validator: (rule, value, callback) => {
              const pattern = /^[\d]{15}(?:[\d]{2})?$/;
              if (value != null && value != "" && !pattern.test(value)) {
                callback(new Error("请输入正确的IMEI码"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        dangerName: [
          {
            validator: (rule, value, callback) => {
              if (
                this.form.result &&
                (this.checkDangerForm.dangerName == null ||
                  this.checkDangerForm.dangerName == "")
              ) {
                callback(new Error("请输入隐患名称"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        hazardDangerType: [
          {
            validator: (rule, value, callback) => {
              if (
                this.form.result &&
                this.checkDangerForm.hazardDangerType == null
              ) {
                callback(new Error("请选择隐患类型"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        dangerLever: [
          {
            validator: (rule, value, callback) => {
              if (
                this.form.result &&
                this.checkDangerForm.dangerLever == null
              ) {
                callback(new Error("请选择隐患等级"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        governanceType: [
          {
            validator: (rule, value, callback) => {
              if (
                this.form.result &&
                this.checkDangerForm.governanceType == null
              ) {
                callback(new Error("请选择治理类型"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        dangerDescribe: [
          {
            validator: (rule, value, callback) => {
              if (
                this.form.result &&
                (this.checkDangerForm.dangerDescribe == null ||
                  this.checkDangerForm.dangerDescribe == "")
              ) {
                callback(new Error("请输入隐患描述"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        cause: [
          {
            validator: (rule, value, callback) => {
              if (
                this.form.result &&
                (this.checkDangerForm.cause == null ||
                  this.checkDangerForm.cause == "")
              ) {
                callback(new Error("请输入原因分析"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        fixDept: [
          {
            validator: (rule, value, callback) => {
              if (this.form.result && this.checkDangerForm.fixDept == null) {
                callback(new Error("请选择整改部门"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        fixBy: [
          {
            validator: (rule, value, callback) => {
              if (this.form.result && this.checkDangerForm.fixBy == null) {
                callback(new Error("请选择整改人"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        endFixTime: [
          {
            validator: (rule, value, callback) => {
              if (this.form.result && this.checkDangerForm.endFixTime == null) {
                callback(new Error("请选择整改期限时间"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getTreeData();
    this.getList();
    this.getDeptTree();
    this.getObject();
  },
  methods: {
    /** 查询排查记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then((response) => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询安全风险对象集合 */
    getObject() {
      this.objectDate = [];
      listObject().then((res) => {
        let objectDate = [];
        res.rows.forEach((element) => {
          objectDate.push({
            id: element.id,
            name: element.name,
          });
        });
        this.objectDate = objectDate;
      });
    },
    /** 查询安全风险单元集合 */
    getUnitDate(objectId) {
      this.unitDate = [];
      listUnit({ objectIds: objectId }).then((res) => {
        let unitDate = [];
        res.rows.forEach((element) => {
          unitDate.push({
            id: element.id,
            name: element.name,
            imei: element.imei,
          });
        });
        this.unitDate = unitDate;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel2() {
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        source: null,
        objectId: null,
        unitId: null,
        content: null,
        checkDept: null,
        checkBy: null,
        isDefend: null,
        imei: null,
        checkBeginTime: null,
        checkEndTime: null,
        checkTime: null,
        checkStatus: null,
        result: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        firmId: null,
      };
      this.checkDangerForm = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getWidth();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.formDisabled = false;
      this.title = "添加排查记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRecord(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.formDisabled = true;
        this.checkDangerForm =
          this.form.checkDanger == null ? {} : this.form.checkDanger;
        this.title = "修改排查记录";
        this.getUnitDate(this.form.objectId);
      });
    },
    /** 转派按钮操作 */
    handleUpdate2(row) {
      this.reset();
      const id = row.id;
      getRecord(id).then((response) => {
        this.form = response.data;
        this.open2 = true;
        this.checkDangerForm =
          this.form.checkDanger == null ? {} : this.form.checkDanger;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.checkDanger = this.checkDangerForm;
          if (this.form.id != null) {
            if (this.form.checkStatus == 0) {
              check(this.form).then((response) => {
                this.$modal.msgSuccess("排查成功");
                this.open = false;
                this.getList();
              });
            } else {
              updateRecord(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            addRecord(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮2 */
    submitForm2() {
      this.form.checkDanger = this.checkDangerForm;
      forwardById(this.form).then((response) => {
        this.$modal.msgSuccess("转派成功");
        this.open2 = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除排查记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRecord(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "syf/record/export",
        {
          ...this.queryParams,
        },
        `record_${new Date().getTime()}.xlsx`
      );
    },
    /** 查询树数据 */
    getTreeData() {
      unitTree().then((res) => {
        this.treeData = res.data;
      });
    },
    //选中树节点
    pitchOnData(val) {
      let unitIds = [];
      val.forEach((item) => {
        unitIds.push(item.id);
      });
      this.queryParams.unitIds = unitIds;
      this.handleQuery();
    },
    //查询用户
    selectUserList(query) {
      if (query !== "") {
        this.loadingUser = true;
        listUser({ nickName: query }).then((res) => {
          this.loadingUser = false;
          this.userList = res.rows;
        });
      } else {
        this.userList = [];
      }
    },
    //获取列表操作列列宽
    getWidth() {
      const status = this.queryParams.checkStatus;
      let width = 150;
      switch (status) {
        case "0":
          width = 75 * 2;
          break;
        case "1":
          width = 75 * 1;
          break;
        case "2":
          width = 75 * 5;
          break;
        case "3":
          width = 75 * 1;
          break;
        case "4":
          width = 75 * 5;
          break;
        case "5":
          width = 75 * 2;
          break;
      }
      this.fixedWidth = width;
    },
    //设置默认imei
    setImei(val) {
      if (this.form.imei == null) {
        this.unitDate.forEach((item) => {
          if (item.id == val) {
            this.form.imei = item.imei;
          }
        });
      }
    },
    //回退
    backById(id) {
      backById(id).then((res) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
    //隐患确认
    confirmById(id) {
      confirmById(id).then((res) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
    //不采纳
    noAdoptionById(id) {
      noAdoptionById(id).then((res) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
  },
};
</script>
